package com.Txunda.fxw.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.other.LeaveMessageAty;
import com.Txunda.fxw.ui.adapter.VideoFeedAdapter;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.http.FeedBack;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建时间：2018/11/16 9:58
 * 作    者：Hzj
 * 描    述：评价列表页
 * 注    意：
 * 修    改：
 * 参    考：
 * 出    自：书山府科技 android
 **/
public class EvaluateAty extends BaseAty {

    @BindView(R.id.evaluate_tv)
    TextView evaluateTv;
    @BindView(R.id.evaluate_lv)
    PullToRefreshListView evaluateLv;
    @BindView(R.id.evaluate_no_data_iv)
    ImageView evaluateNoDataIv;
    private VideoFeedAdapter videoFeedAdapter;
    private FeedBack feedBack;
    private int p = 1;
    private String count = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        evaluateLv.setAdapter(videoFeedAdapter);
        evaluateLv.setEmptyView(evaluateNoDataIv);
        evaluateLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                p = 1;
                getEvaluate();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                p++;
                getEvaluate();
            }
        });
    }

    @Override
    @OnClick({R.id.evaluate_tv})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.evaluate_tv:
                toEvaluate();
                break;
            default:
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected void initialized() {
        type = getIntent().getStringExtra("type");
        if (getIntent().getStringExtra("count") != null) {
            count = getIntent().getStringExtra("count");
        }

        videoFeedAdapter = new VideoFeedAdapter(this);
        feedBack = new FeedBack();
    }

    @Override
    protected void requestData() {
        p = 1;
        getEvaluate();
    }

    private void getEvaluate() {
        Log.d("zdl", "========获取到的type========" + type);
        feedBack.feedbackList(p, type, this);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map)) {
            return;
        }
        if (requestUrl.contains("FeedbackList")) {
            Log.d("zdl", "=======评价列表======== " + jsonStr);
            List<Map<String, String>> data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
            if (1 == p) {
                videoFeedAdapter.clear();
            }
            if (!ListUtils.isEmpty(data)) {
                videoFeedAdapter.updata(data);
            }
            evaluateLv.onRefreshComplete();
            return;
        }
        if (requestUrl.contains("comment")) {
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data)) {
                return;
            }
            application.setUserInfo(data);

            Bundle bundle = new Bundle();
            bundle.putString("type", type);
            startActivityForResult(LeaveMessageAty.class, bundle, 100);
        }
    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        super.onError(requestUrl, error);
        evaluateLv.onRefreshComplete();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        boolean success = data.getBooleanExtra("success", false);
        if (RESULT_OK == resultCode && 100 == requestCode && success) {
            p = 1;
            getEvaluate();
        }
    }
}
